<?php
session_start();

include("config.php");

$erreur = 0;
$form = 1;


	//On verifie si l'on a un premier acces à la page
	if(isset($_POST['text']))
	{
		//On verifie que tous les champs soient remplis
		if(empty($_POST['resume']) || empty($_POST['text'])  )
		{
			$erreur = 1;
			echo "Empty fields are not allowed <br>";
		}
		else
		{	
			//On affecte la valeur des champs à nos variables
			$resume = addslashes($_POST['resume']);
			$text = addslashes($_POST['text']);


			//On se connecte à la base de donnée
			$link = mysql_connect ('localhost', DB_USER_NAME, DB_PASSWORD) or die ("Cannot join database");
			mysql_select_db (DB_NAME);

			//On verifie si le pseudo n'existe pas déjà
			$requete = "INSERT INTO stream(id_stream,user,resume,message,date) 
									VALUES ('', '" . $_SESSION['pseudo'] . "', '" . $resume . "',
									'" . $text . "', '" . time() . "')";
			mysql_query ($requete);

			mysql_close($link);

			echo '<meta http-equiv="refresh" content="0;URL=index.php?profil=1">';
			//header('Location: index.php?page=profil');   
			$form = 0;
		
		}
	}


		if($form)
		{
			if($erreur)
			{
			?>
				<form method="post" action="index.php?profil=1">
				<p>Resume: <input type="text" name="resume" value="<? echo $_POST['resume']; ?>"/></p>
				<p>Text:</p> <textarea type="textarea" name="text" cols="50" rows="10"><? echo $_POST['text']; ?></textarea>
				<p><input type="submit" value="Valider" /></p>
				</form>
			<?php
			}
			else
			{
			?>
				<form method="post" action="index.php?profil=1">
				<p>Resume: <input type="text" name="resume" /></p>
				<p>Text:</p> <textarea name="text" cols="50" rows="10" ></textarea>
				<p><input type="submit" value="Valider" /></p>
				</form>
				</form>
			<?php
			}
		}
?>
	

